package com.wjf.accesscontrol.util.sdkUtils

import android.content.Context
import android.speech.tts.TextToSpeech
import com.common.apiutil.pos.CommonUtil
import com.wjf.accesscontrol.util.LogUtils
import com.wjf.accesscontrol.util.handler.HandlerUtil
import java.util.Locale

/**
 * @Description
 * @Author WuJianFeng
 * @Date 2024/2/4 11:31
 *
 */
const val mRelayType = 20000015

object TelpoUtils: TextToSpeech.OnInitListener {

    private var mCommonUtil: CommonUtil? = null
    private var textToSpeech: TextToSpeech? = null

    fun setCommonUtil(context: Context){
        mCommonUtil = CommonUtil(context)
        textToSpeech = TextToSpeech(context, this)

    }

    /**
     * 继电器上电：开门
     */
    fun setRelayPowerOn(){
        textToSpeech?.speak("门已开", TextToSpeech.QUEUE_FLUSH, null, null)

        HandlerUtil.instance.doorLock()
        val result = mCommonUtil?.setRelayPower(mRelayType,1)
        LogUtils.d("__TelpoUtils-on","${result}")
    }

    /**
     * 继电器下电：关门
     */
    fun setRelayPowerOff(){
        val result = mCommonUtil?.setRelayPower(mRelayType,0)
        LogUtils.d("__TelpoUtils-off","${result}")
    }

    override fun onInit(status: Int) {
        if (status == TextToSpeech.SUCCESS) {
            val result = textToSpeech?.setLanguage(Locale.CHINA)
        }
    }

    fun clear(){
        if (textToSpeech?.isSpeaking == true) {
            textToSpeech?.stop()
        }
        textToSpeech?.shutdown()
        textToSpeech = null
    }
}